<?php
/**
 *	Actions for Messages.
 *	@package		tools.BugTracker
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
/**
 *	Actions for Messages.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_Core_DefinitionAction
 *	@uses			Message
 *	@uses			User
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
class Action_Message extends CMF_Krypton_Core_DefinitionAction
{
	public function __construct()
	{
		parent::__construct();
		$this->loadLanguage( 'message' );
		$this->addAction( 'sendMessage' );
		$this->addAction( 'removeMessage' );
		$this->addAction( 'replyMessage' );
		$this->addAction( 'forwardMessage' );
		$auth			= $this->registry->get( 'auth' );
		$this->current	= $auth->getCurrentUser();
	}

	/**
	 *	Forwards a Message to several Receivers.
	 *	@access		protected
	 *	@return		void
	 */
	protected function forwardMessage()
	{
		$words		= $this->words['message']['msg'];
		$messageId	= $this->request->get( 'messageId' );
		$receivers	= $this->request->get( 'receivers' );
		$subject	= $this->request->get( 'subject' );
		$body		= $this->request->get( 'body' );

		try
		{
			if( !$messageId )
				throw new InvalidArgumentException( '...' );
			foreach( $receivers as $receiver )
			{
				Logic_Message::sendMessage(
					$this->current,
					$receiver,
					$subject,
					$body
				);
			}
			$status	= Logic_Message::getStatus( $messageId );
			if( $status == 1 )
				Logic_Message::setStatus( $messsageId, 3 );
			else if( $status == 2 )
				Logic_Message::setStatus( $messsageId, 4 );
			$this->messenger->noteSuccess( $words['success_forwarded'] );
			$this->request->remove( 'action' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e );
		}
	}

	/**
	 *	Removes a Message.
	 *	@access		protected
	 *	@return		void
	 */
	protected function removeMessage()
	{
		$words		= $this->words['message']['msg'];
		$messageId	= $this->request->get( 'messageId' );
		if( !$messageId )
			return;
		try
		{
			Logic_Message::removeMessage( $messageId, $this->current );
			$request->remove( "messageId" );
			$this->messenger->noteSuccess( $words['success2'] );
		}
		catch( Exception $e )
		{
			$this->handleException( $e );
		}
	}

	/**
	 *	Replies a Message to several receivers.
	 *	@access		protected
	 *	@return		void
	 */
	protected function replyMessage()
	{
		$words		= $this->words['message']['msg'];
		$messageId	= $this->request->get( 'messageId' );
		if( !$messageId )
			return;

		$message	= Logic_Message::getMessage( $messageId );
		$receivers	= (array) $this->request->get( 'receivers' );
		$subject	= $this->request->get( 'subject' );
		$body		= $this->request->get( 'body' );
		array_unshift( $receivers, $message['source'] );

		try
		{
			foreach( $receivers as $receiver )
			{
				Logic_Message::sendMessage(
					$this->current,
					$receiver,
					$subject,
					$body
				);
			}
			if( $message['status'] == 1 )
				Logic_Message::setStatus( $messageId, 2 );
			else if( $message['status'] == 3 )
				Logic_Message::setStatus( $messageId, 4 );
			$this->messenger->noteSuccess( $words['success1'] );
			$this->request->remove( 'action' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e );
		}
	}

	/**
	 *	Sends a Message to several Receivers.
	 *	@access		protected
	 *	@return		void
	 */
	protected function sendMessage()
	{
		$mail		= $this->registry->get( 'mail' );
		$words		= $this->words['message']['msg'];

		$receivers	= $this->request->get( 'receivers' );
		$subject	= $this->request->get( 'subject' );
		$body		= $this->request->get( 'body' );

		try
		{
			foreach( $receivers as $receiver )
			{
				Logic_Message::sendMessage(
					$this->current,
					$receiver,
					$subject,
					$body
				);
				$this->messenger->noteSuccess( $words['success1'], $user['username'] );
				$user	= Logic_User::getUserData( $receiver );
				if( $user['notify'] )
				{
					if( $mail->sendMail( $this->current, $receiver, $subject, $body ) )
						$this->messenger->noteSuccess( $words['success3'], $user['username'] );
					else
						$this->messenger->noteError( $words['error2'], $user['username'] );
				}
				$this->request->remove( 'action' );
			}
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'message', 'sendMessage' );
		}
	}
}
?>
